package com.emex.app.stuff.provider;

import com.emex.app.shared.data.Car;
import com.emex.app.shared.ws.CarWebService;
import com.emex.app.stuff.utils.WebServiceHelper;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.Service;

/**
 *
 * @author Sergey Karavaev
 */
public class CarDataProvider extends Service {

    private CarWebService service = null;

    private static CarDataProvider instance = null;

    /**
     * Constructor
     */
    private CarDataProvider() throws MalformedURLException {
        super(new URL(WebServiceHelper.getDefault().getWebServiceURL("web", "Car")),
                WebServiceHelper.getDefault().getWebServiceQName("Car"));
        service = getPort(CarWebService.class);
    }

    /**
     * Get instance of data provider.
     *
     * @return data provider instance
     */
    public static synchronized CarDataProvider getInstance() {
        if (instance == null) {
            new RequestWebService() {
                @Override
                protected void doRequestBody() throws MalformedURLException {
                    instance = new CarDataProvider();
                }
            }.doRequest();
        }
        return instance;
    }

    public BindingProvider getBindingProvider() {
        return (BindingProvider) service;
    }

    public int deleteByPrimaryKey(Integer id) {
        return service.deleteByPrimaryKey(id);
    }

    public Car insert(Car record) {
        return service.insert(record);
    }

    public Car selectByPrimaryKey(Integer id) {
        return service.selectByPrimaryKey(id);
    }

    public Car updateByPrimaryKey(Car record) {
        record = service.updateByPrimaryKey(record);
        return record;
    }

    public List<Car> getAllCarsByClientId(Integer id) {
        return service.getAllCarsByClientId(id);
    }
}
